package pro.lepers.portfolio.view.contents.portfolio.data {
	import fr.digitas.flowearth.core.IIterator;
	import fr.digitas.flowearth.mvc.address.structs.INode;
	import fr.digitas.flowearth.mvc.address.structs.descriptor.INodeDescriptor;

	import pro.lepers.portfolio.view.contents.portfolio.data.contents.AbstractContentData;
	import pro.lepers.portfolio.view.contents.portfolio.data.contents.ProjectContentFactory;
	import pro.lepers.portfolio.view.contents.portfolio.data.contents.ProjectContentProvider;

	import flash.utils.Dictionary;

	/**
	 * @author plepers
	 */
	public class ProjectProvider implements INodeDescriptor {

		
		
		public function ProjectProvider( node : INode, datas : XML ) {
			_node = node;
			_build( datas );
		}

		
		public function getItems() : IIterator {
			return new ProjectDatasIterator( _aItems );
		}

		public function getItem( id : String ) : ProjectDatas {
			if( _dItems[ id ] == undefined )
				throw new Error( "ProjectProvider - no datas corresponding to id '" + id +"'" );
			return _dItems[ id ];
		}

		public function getItemIndex( id : String ) : int {
			var item : ProjectDatas = _dItems[id];
			return _aItems.indexOf( item );
		}

		public function getItemAt( index : int ) : ProjectDatas {
			if( index > -1 && index < _aItems.length )
				return _aItems[ index ];
			return null;
		}

		
		
		public function get node() : INode {
			return _node;
		}

		public function dispose() : void {
			
			for (var i : int = 0; i < _aItems.length; i++) {
				_aItems[ i ].dispose( );
				_aItems[ i ] = null;
			}
			_aItems = null;
			_dItems = null;
		}

		
		public function compile() : void {
			
			for each (var item : ProjectDatas in _aItems ) {
				item._contentsProvider.setProject( item );
			}
			
		}
		
		private function _build(datas : XML) : void {

			var list : XMLList = datas.projects.project;

			_aItems = new Vector.<ProjectDatas>( list.length( ) , true );	
			_dItems = new Dictionary( );
			
			
			var item : ProjectDatas;
			var c : int = 0;
			
			for each (var idata : XML in list ) {
				item = new ProjectDatas( );
				
				item._id = String( idata.@id );
				item._date = new Date( parseInt( idata.date.@value ) );
				item._title = idata.title.text( );
				item._desc = idata.desc.text( );
				item._rootNode = _node;
				
				item._contentsProvider = _getContentProvider( idata.contents[0] );
//				item._contentsProvider.setProject( item );
				
				_aItems[ c ] = item;
				_dItems[ item.id ] = item;
				c ++;
			}
		}
		
		private function _getContentProvider( cdata : XML ) : ProjectContentProvider {
			
			var list : XMLList = cdata.content;
			var v : Vector.<AbstractContentData> = new Vector.<AbstractContentData>();
			var item : AbstractContentData;
			
			for each (var cxml : XML in list ) {
				item = ProjectContentFactory.getContentData( cxml );
				v.push( item );
			}
			
			return new ProjectContentProvider( v );
		}
		

		private var _aItems : Vector.<ProjectDatas>;
		private var _dItems : Dictionary;

		private var _node : INode;

		//_____________________________________________________________________________
		//															    INodeDescriptor

		public function getChilds() : IIterator {
			return getItems( );
		}

		public function getId() : String {
			return _node.getId( );
		}

		public function getDefaultId() : String {
			return _aItems[0]._id;
		}
	}
}

import fr.digitas.flowearth.core.IIterator;

import pro.lepers.portfolio.view.contents.portfolio.data.ProjectDatas;

class ProjectDatasIterator implements IIterator {

	public function ProjectDatasIterator( a : Vector.<ProjectDatas> ) {
		_a = a;
		_c = 0;
	}

	public function next() : Object {
		return _a[_c ++];
	}

	public function hasNext() : Boolean {
		return _a.length > _c;
	}

	private var _a : Vector.<ProjectDatas>;
	private var _c : int;
}

